package com.neu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.mapper.GoodsMapper;
import com.neu.mapper.GoodsTypeMapper;
import com.neu.po.Goods;
import com.neu.po.GoodsExample;
import com.neu.po.GoodsExample.Criteria;
import com.neu.po.GoodsType;
import com.neu.po.GoodsTypeExample;

@Service
public class GoodsServiceImpl implements GoodsService {
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private GoodsTypeMapper goodsTypeMapper;
	
	@Override
	public int insert(Goods goods) {
		return goodsMapper.insert(goods);
	}

	@Override
	public int update(Goods goods) {
		return goodsMapper.updateByPrimaryKey(goods);
	}

	@Override
	public int typeDel(int id) {
		return goodsMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PageInfo<Goods> getPaged(int pageNum, int pageSize, String keyword) {
		GoodsExample example = new GoodsExample();
		Criteria c1 = example.createCriteria();
		
		c1.andGoodsnameLike("%"+keyword+"%");
		
		PageHelper.startPage(pageNum,pageSize);
		List<Goods> list = goodsMapper.selectByExample(example);
		
		PageInfo<Goods> pageInfo = new PageInfo<>(list);
		
		return pageInfo;
	}

	@Override
	public int batchDel(List<Integer> ids) {
		GoodsExample example = new GoodsExample();
		example.or().andIdIn(ids);
		return goodsMapper.deleteByExample(example);
	}

	@Override
	public List<GoodsType> getAll() {
		GoodsTypeExample example = new GoodsTypeExample();
		List<GoodsType> list = goodsTypeMapper.selectByExample(example);
		return list;
	}

	@Override
	public int insertType(GoodsType goodsType) {
		
		return goodsTypeMapper.insert(goodsType);
	}

}
